#include <iostream>
#include <vector>
#include <string>

using namespace std;

// 优化：遇到减号的时候直接讲相反数压入栈

int calculate(string s)
{
    vector<int> stk;
    char preSign = '+';
    int num = 0;
    int n = s.length();
    for (int i = 0; i < n; ++i) {
        if (isdigit(s[i])) {
            num = num * 10 + int(s[i] - '0');
        }
        if (!isdigit(s[i]) && s[i] != ' ' || i == n - 1) {
            switch (preSign) {
                case '+':
                    stk.push_back(num);
                    break;
                case '-':
                    stk.push_back(-num);
                    break;
                case '*':
                    stk.back() *= num;
                    break;
                default:
                    stk.back() /= num;
            }
            preSign = s[i];
            num = 0;
        }
    }
    return accumulate(stk.begin(), stk.end(), 0);
}

int cal(vector<int> &nst, vector<char> &sst)
{
    int r;
    switch (sst[sst.size() - 1])
    {
    case '+':
        r = *(nst.end() - 2) + *(nst.end() - 1);
        break;

    case '-':
        r = *(nst.end() - 2) - *(nst.end() - 1);
        break;

    case '*':
        r = *(nst.end() - 2) * *(nst.end() - 1);
        break;

    case '/':
        r = *(nst.end() - 2) / *(nst.end() - 1);
        break;
    
    default:
        break;
    }
    nst.pop_back(); nst.pop_back(); sst.pop_back();
    return r;
}

int myCalculate(string s)
{
    vector<int> nst; vector<char> sst; int i = 0, pre = i;
    while (i < s.size())
    {
        while (i < s.size() && s[i] == ' ') i++;
        // 存数字
        pre = i;
        while (i < s.size() && s[i] >= '0' && s[i] <= '9') i++;
        nst.push_back(stoi(s.substr(pre, i - pre)));
        // 存符号
        while (i < s.size() && s[i] == ' ') i++;
        if (i == s.size())
        {
            while (!sst.empty())
            {
                nst.push_back(cal(nst, sst));
            }
        }else if (((s[i] == '+' || s[i] == '-') && !sst.empty()))
        {
            while (!sst.empty())
            {
                nst.push_back(cal(nst, sst));
            }
            sst.push_back(s[i]);
        }else if ((s[i] == '*' || s[i] == '/') && !sst.empty() && (*(sst.end() - 1) == '*' || *(sst.end() - 1) == '/'))
        {
            while (!sst.empty())
            {
                nst.push_back(cal(nst, sst));
            }
            sst.push_back(s[i]);
        }else
        {
            sst.push_back(s[i]);
        }
        i++;
    }
    return nst[0];
}

int main()
{
    string s = "1*2-3/4+5*6-7*8+9/10";
    cout << calculate(s) << endl;
    return 0;
}
